import request from "@/utils/request";

// 查询字典数据列表
export function listData(query) {
  return request({
    url: "/system/dict/data/list",
    method: "get",
    params: query
  });
}

// 查询字典数据详细
export function getData(dictCode) {
  return request({
    url: `/system/dict/data/${dictCode}`,
    method: "get"
  });
}

// 根据字典类型查询字典数据信息
export function getDicts(dictType) {
  return request({
    url: `/system/dict/data/type/${dictType}`,
    method: "get"
  });
}

// 新增字典数据
export function addData(data) {
  return request({
    url: "/system/dict/data",
    method: "post",
    data
  });
}

// 修改字典数据
export function updateData(data) {
  return request({
    url: "/system/dict/data",
    method: "put",
    data
  });
}

// 删除字典数据
export function delData(dictCode) {
  return request({
    url: `/system/dict/data/${dictCode}`,
    method: "delete"
  });
}
// 查询列表数据
export function getListData(params, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/list`,
    method: 'get',
    params: params,
    headers: {
      showLoading: true,
      showUploading: false,
    }
  })
}
// 获取下拉框数据
export function getSelectorData(name, params, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/selector/` + name,
    method: 'get',
    params: params,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  })
}
// 提交
export function submit(data, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit`,
    method: 'post',
    data: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 提交其他
export function submit_qt(data, config,qt) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/${qt}`,
    method: 'post',
    data: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 提交删除
export function submit_delete(data, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/delete`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 提交删除其他
export function submit_qtsj(data, config,qt) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/${qt}`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 删除附件
export function submit_delfile_fj(data, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/delfile_fj`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 获取附件列表
export function getListData_fj(params, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/list/fj`,
    method: 'get',
    params: params,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  })
}
// 删除有名字附件
export function submit_delfile_name(data, config,name) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/delfile_${name}`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 获取有名字附件列表
export function getListData_name(params, config,name) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/list/${name}`,
    method: 'get',
    params: params,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  })
}
// 删除文档
export function submit_delfile(data, config) {
  return request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config}/submit/delfile`,
    method: 'post',
    params: data,
    headers: {
      showLoading: false,
      showUploading: true,
    }
  })
}
// 获取文件
export function downloadFile(data, config) {
  request({
    url: `cenertech-inner-syfxxt-khfw/pd/${config || 'zxrcsw/zscqgl'}/miniofileinfo`,
    method: 'get',
    params: data,
    headers: {
      showLoading: false,
      showUploading: false,
    }
  }).then((res) => {
    console.log(res, '4444444444');

    if (res.rows && res.rows.length > 0) {
      var fileinfo = res.rows[0];
      request({
        url: "/file/download/" + fileinfo.path,
        method: "get",
        responseType: "blob",
        headers: {
          showLoading: false,
          showUploading: false,
        }
      }).then((filedata) => {
        console.log(filedata, '55');

        // 检查filedata是否为Blob对象
        if (filedata instanceof Blob) {
          // 创建一个URL对象
          const url = window.URL.createObjectURL(filedata);
          // 创建一个<a>元素
          const a = document.createElement('a');
          a.href = url;
          a.download = fileinfo.filename;
          document.body.appendChild(a);
          a.click();
          // 释放URL对象
          window.URL.revokeObjectURL(url);
          document.body.removeChild(a);
        } else {
          // 如果不是Blob对象，尝试将响应数据转换为文本并解析
          filedata.text().then(resText => {
            const rspObj = JSON.parse(resText);
            const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode.default;
            Message.error(errMsg);
          }).catch(error => {
            console.error('解析响应文本时出错:', error);
            Message.error('无法解析响应文本');
          });
        }
      }).catch((r) => {
        console.error('下载文件时出错:', r);
        Message.error("下载文件出现错误");
      });
    } else {
      console.error('没有找到文件信息:', res);
      Message.error("下载文件出现错误");
    }
  }).catch((r) => {
    console.error('获取文件信息时出错:', r);
    Message.error("下载文件出现错误");
  });
}
// 上传文件
export function uploadFile(data) {
  return request({
    url:'/file/upload',
    method: "post",
    data,
  });
}
// 导入
export function importExcel(data,config) {
  return request({
    url:`cenertech-inner-syfxxt-khfw/pd/${config}/import/excel`,
    method: "post",
    data,
  });
}
// 导出
export function exportExcel(download, data, filename, config) {
  download(
    `cenertech-inner-syfxxt-khfw/pd/${config}/export/excel`,
    data,
    filename,
  );
}
// 下载模版
export function exportExcel_qt(download, data, filename, config,qt) {
  download(
    `cenertech-inner-syfxxt-khfw/pd/${config}/export/excel/${qt}`,
    data,
    filename,
  );
}